﻿using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.ViewModel;
using IPR_StudyMeetingMrgSys.Common;

using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web;
using System.Web.Http;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    public class TrainClassRoomController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        BLL.TrainClassRoom BllTrainClassRoom = new BLL.TrainClassRoom();
        [HttpPost]
        [Route("api/TrainClassRoom/Add")]
        public IHttpActionResult AddTrainClassRoom(Model.TrainClassRoom model)
        {
            log.Info($"CRID:{model.CRID};CRName:{model.CRName};");

            ResultMsg resultMsg = new ResultMsg();
            //验证教室ID CRID是否存在
            if (BllTrainClassRoom.Exists(model.CRName))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = "已经存在该教室名称";
                resultMsg.Data = "";
            }
            else
            {
            int CRID= new BLL.SysManage().GetMaxId("TrainClassRoom", "CRID");
                model.CRID = CRID;
                if (BllTrainClassRoom.Add(model)>0)
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = null;
                }
            }

            return Json(resultMsg);
        }
        /// <summary>
        /// 查找部门对应的所有的教室，部门可以为空
        /// </summary>
        /// <param name="DeptID"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/TrainClassRoom/Sel")]
        public IHttpActionResult GetTrainClassRoomList([FromBody]string DeptID)
        {
            log.Info($"获取教室列表");
            ResultMsg resultMsg = new ResultMsg();

            string swhere = "";
            if (!string.IsNullOrEmpty(DeptID))
            {
                if (DeptID == "0")//
                {
                    DeptID = LoginUser.DepID.ToString();
                }
                swhere = " DeptID=" + DeptID;
            }
            DataTable dt= BllTrainClassRoom.GetList(swhere).Tables[0];
            if (dt.Rows.Count > 0)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = dt;
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "没有获取到教室信息";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }

        
        

        [HttpPost]
        [Route("api/TrainClassRoom/GetTrainClassRoomInfo")]
        public IHttpActionResult GetTrainClassRoomInfo()
        {
            log.Info($"获取教室列表并形成树结构所需的json串");
            ResultMsg resultMsg = new ResultMsg();
            DataTable dt = BllTrainClassRoom.GetAllList().Tables[0];
            if (dt.Rows.Count > 0)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = dt;
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "没有获取到教室信息";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
        [HttpPost]
        [Route("api/TrainClassRoom/Del")]
        public IHttpActionResult DelTrainClassRoom([FromBody]long CRID)
        {
            log.Info($"删除教室信息");
            ResultMsg resultMsg = new ResultMsg();
            
            if (BllTrainClassRoom.Delete(CRID))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "";
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "删除教室信息失败";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
        [HttpPost]
        [Route("api/TrainClassRoom/Edit")]
        public IHttpActionResult UpdateTrainClassRoom(Model.TrainClassRoom model)
        {
            log.Info($"更新教室信息");
            ResultMsg resultMsg = new ResultMsg();
            if (BllTrainClassRoom.Update(model))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "";
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "更新教室信息失败";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
    }
}
